package com.java.mybatis.controller;

import com.java.mybatis.entity.Supplier;
import com.java.mybatis.service.SupplierService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (Supplier)表控制层
 *
 * @author makejava
 * @since 2021-01-15 17:10:27
 */
@Controller
@RequestMapping("/supplier/")
public class SupplierController {
    /**
     * 服务对象
     */
    @Resource
    private SupplierService supplierService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Supplier selectOne(Integer id) {
        return this.supplierService.queryById(id);
    }

    /**
     * 显示信息
     * @param model
     * @param map
     * @return
     */
    @RequestMapping("selectSupplier")
    public String selectSupplier(Model model,Map map){
        List<Supplier> suppliers = supplierService.GetAll ();
        for (Supplier supplier : suppliers) {
            System.out.println(suppliers);
        }
        map.put("selectSupplier",suppliers);
        Integer integer = null;
        try {
            integer = supplierService.supplierCount();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println( integer);
        model.addAttribute("count",integer);
        return "System/category";
    }

    /**
     * 模糊查询
     */
    @RequestMapping("selectSupplierLike")
    public String selectSupplierLike(Model model,String str) throws Exception{
        //如果str==""   redirect:/supplier/selectSupplier?page=1
        //System.out.println("--------"+str);
        List<Supplier> suppliers = supplierService.selectSupplier("%" + str + "%");
        model.addAttribute("selectSupplier",suppliers);
        //模糊查询的中条数
        Integer integer = null;
        try {
            integer = supplierService.selectSupplierLikeCount("%"+str+"%");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println( integer);
        model.addAttribute("count",integer);
        return "System/category";
    }

    /**
     * 单选删除
     * @param supplierId
     */
    @RequestMapping("updateSupplierCode")
    public void updateSupplierCode(Integer supplierId){
        // id 1
        int i = supplierService.updateSupplierCode(supplierId);
        //return "redirect:/supplier/updateSupplierCode";
    }

    /**
     * 多选删除
     * @param str
     */
    @RequestMapping("updateSupplierCodeAll")
    public String updateSupplierCodeAll(String str){
        // 2,3,4
        String[] split = str.split(",");
        for (String s : split) {
            //s     2  3  4
            supplierService.updateSupplierCode(Integer.parseInt(s.trim()));
        }
        return "redirect:/supplier/selectSupplier";
    }
}